package com.lianbang.pharmacy.module.medicine.detail

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.Menu
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.SpanUtils
import cn.ubest.mvvmcore.utils.ToastUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.databinding.ActivityMedicineDetailBinding
import com.lianbang.pharmacy.module.main.MedicineSupplierActivity
import com.lianbang.pharmacy.vm.MedicineVM
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.listener.OnPageChangeListener
import kotlinx.android.synthetic.main.activity_medicine_detail.*

class MedicineDetailActivity : BaseActivity<MedicineVM, ActivityMedicineDetailBinding>() {

    companion object {
        fun start(context: Context?) {
            context?.also { c ->
                val starter = Intent(c, MedicineDetailActivity::class.java)
                if (c !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                c.startActivity(starter)
            }
        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_medicine_detail

    }

    override fun initData(intent: Intent) {

    }

    override fun initView() {
        val bannerList = mutableListOf("", "", "");
        val adapter = object : BannerImageAdapter<String>(bannerList) {
            override fun onBindView(
                holder: BannerImageHolder,
                data: String,
                position: Int,
                size: Int
            ) {
                //图片加载自己实现
                val color = when {
                    position % 3 == 0 -> {
                        R.color.colorPrimary
                    }
                    position % 3 == 1 -> {
                        R.color.black
                    }
                    else -> {
                        R.color.C_FFD214
                    }
                }
                holder.imageView.setImageResource(color)
            }
        }
        mBinding?.banner?.setAdapter(adapter)?.addBannerLifecycleObserver(this)
            ?.addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrollStateChanged(state: Int) {

                }

                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    tv_indicator.text = "${position + 1}/${bannerList.size}"
                }
            })
            ?.setOnBannerListener { data, position -> ToastUtils.toast("点击了第${position+1}页")}
        tv_indicator.text = "1/${bannerList.size}"

        tv_title.text = "抗之霸 阿莫西林胶囊抗之霸 阿莫西林胶囊抗之霸 阿莫西林胶囊抗之霸 阿莫西林胶囊抗之霸 阿莫西林胶囊"
        tv_desc.text = "清热解毒、利咽止痛、生津润喉。用于风热上攻，肺胃热盛所致急、慢性咽喉炎。防止螺栓杆菌，并且-使与幽门螺旋杆菌复发的各种症状，综合性…"
        tv_price.text =
            SpanUtils().append("¥").append("10.").setFontSize(26, true).append("76").create()
        tv_city.text = "武汉市"
        tv_area.text = "洪山区"
        tv_remain_count.text = "89"
        tv_valid_until.text = "2021.02.08"
        tv_approval_number.text = "国药准字B20050066"
        tv_specification.text = "0.5gx12片x2板/盒"
        btn_expansion.setOnClickListener {

        }

        val titleList = mutableListOf("基本信息", "说明书", "服务保障")
        val fragmentList = mutableListOf(
            MedicineDetailInfoFragment.newInstance(),
            MedicineDetailInfoFragment.newInstance(),
            MedicineDetailInfoFragment.newInstance()
        )
        tl_tab.setupWithViewPager(vp_pager)
        tl_tab.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val text = tab.text.toString().trim()
                tab.text = SpanUtils().append(text).setBold().create()
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                val text = tab.text.toString().trim()
                tab.text = SpanUtils().append(text).create()
            }

            override fun onTabReselected(tab: TabLayout.Tab) {}
        })
        vp_pager.adapter = object : FragmentStatePagerAdapter(
            supportFragmentManager,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getPageTitle(position: Int): CharSequence? {
                return titleList[position]
            }

            override fun getItem(position: Int): Fragment {
                return fragmentList[position]
            }

            override fun getCount(): Int {
                return fragmentList.size
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_medicine_detail, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.btn_shop -> ToastUtils.toast("购物车")
            R.id.btn_more -> ToastUtils.toast("更多")
        }
        return super.onOptionsItemSelected(item)
    }


    override fun loadData() {

    }
}
